package test;

import java.io.IOException;
import java.util.Random;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.statistics.HistogramDataset;
import org.jfree.data.statistics.HistogramType;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

@SuppressWarnings("serial")
public class ProvaGrafoHistogram extends ApplicationFrame {
    static Random random = new Random();

    public ProvaGrafoHistogram(String title) {
        super(title);    
        HistogramDataset dataset = createDataset();
        JFreeChart chart = createChart(dataset);
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        chartPanel.setMouseZoomable(true, false);
        setContentPane(chartPanel);
    }
    
    private HistogramDataset createDataset() {
        HistogramDataset dataset = new HistogramDataset();
        dataset.setType(HistogramType.RELATIVE_FREQUENCY);
        dataset.addSeries("H1", gaussianData(1000, 3.0), 20);
        dataset.addSeries("H0", gaussianData(1000, 0), 20);   
        return dataset;     
    }
    
    private JFreeChart createChart(HistogramDataset dataset) {
        JFreeChart chart = ChartFactory.createHistogram(
            "Histogram Demo", 
            null, 
            null, 
            dataset, 
            PlotOrientation.VERTICAL, 
            true, 
            false, 
            false
        );
        chart.getXYPlot().setForegroundAlpha(0.75f);
        return chart;
    }
    
    private static double[] gaussianData(int size, double shift) {
        double[] d = new double[size];
        for (int i = 0; i < d.length; i++) {
            d[i] = random.nextGaussian() + shift;
        }
        return d;
    }
    
    public static void main(String[] args) throws IOException {
        
    	ProvaGrafoHistogram demo = new ProvaGrafoHistogram("Histogram");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
        
    }
}
